package simple.cashflow.service.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import simple.cashflow.model.Golongan;
import simple.cashflow.service.GolonganService;

@Service("golonganService")
@Transactional(readOnly=true)
public class GolonganServiceImpl implements GolonganService{
	
	@Autowired
    private SessionFactory sessionFactory;
    private String hqlCariDariKode = "from Golongan g where g.kodeGol =:kode";
    private String hqlCariSemua = "from Golongan";

	@Override
	@Transactional(readOnly=false)
	public void simpanGolongan(Golongan golongan) {
		sessionFactory.getCurrentSession().saveOrUpdate(golongan);
	}

	@Override
	@Transactional(readOnly=false)
	public void hapusGolongan(Golongan golongan) {
		sessionFactory.getCurrentSession().delete(golongan);
	}

	@Override
	public Golongan cariGolonganDariKode(String kodeGol) {
		return (Golongan) sessionFactory.getCurrentSession().createQuery(hqlCariDariKode)
					.setParameter("kode", kodeGol).uniqueResult();
	}

	@Override
	public List<Golongan> cariSemuaGolongan() {
		return sessionFactory.getCurrentSession().createQuery(hqlCariSemua).list();
	}
	
}
